//
//  ConGoodsTypeViewController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/4/7.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConGoodsTypeViewController.h"
#import "BusinessNetwork.h"
#import "MBProgressHUD+MJ.h"
#import "SZLSellerAddGoodsViewController.h"
#import "RTRootNavigationController.h"
#import "ConShoppingMallsController.h"

@interface ConGoodsTypeViewController ()

@property (nonatomic,strong) GoodsTypeButton *selectBtn;
@property (nonatomic,strong) NSMutableArray *firstypes;

@end

@implementation ConGoodsTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品分类";
    self.view.backgroundColor = RGBColor(239, 239, 244);
    [self setNavi];
    [self getGoodsType];
}
- (void)setNavi
{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 30)];
    btn.titleLabel.font = Font(16);
    [btn setTitle:@"保存" forState:UIControlStateNormal];
    [btn sizeToFit];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(saveClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
}
- (void)setUpBtns
{
    NSInteger row;
    NSInteger column;
    float rowMargin = 10;
    float colMargin = 10;
    float width = (ScreenW-rowMargin*5)/4.0;
    float height = 30;
    for (NSInteger i=0; i<self.secondTypes.count; i++){
        HomeGoodsTypeModel *model = self.secondTypes[i];
        row = i/4;
        column = i%4;
        GoodsTypeButton *btn = [[GoodsTypeButton alloc]initWithFrame:CGRectMake(rowMargin+(width+rowMargin)*column, 10+(height+colMargin)*row, width, height)];
        btn.titleLabel.font = Font(12);
        [btn setTitle:model.productTypeName forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.backgroundColor = RandomColor;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.model = model;
        [self.view addSubview:btn];
    }
}
- (void)saveClick:(UIButton *)btn
{
    if (!self.selectBtn) {
        [MBProgressHUD showError:@"请选择一个分类"];
        return;
    }
    if (self.selectBlock) {
        self.selectBlock(self.selectBtn.model);
    }
    [self.rt_navigationController popViewControllerAnimated:YES complete:nil];
}
- (void)btnClick:(GoodsTypeButton *)btn
{
    if (btn == self.selectBtn) return;
    btn.selected = YES;
    self.selectBtn.selected = NO;
    self.selectBtn = btn;
}
- (void)getGoodsType
{
    [self showClearBgWaiter];
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"type"] = @(1);
    [BusinessNetwork getAdderGoodsTypeWithParame:parame response:^(BOOL success, NSString *msg, NSArray *types) {
        [self hideWaiter];
        if (success) {
            NSInteger row;
            NSInteger column;
            float rowMargin = 10;
            float colMargin = 10;
            float width = (ScreenW-rowMargin*5)/4.0;
            float height = 30;
            for (NSInteger i=0; i<types.count; i++){
                HomeGoodsTypeModel *model = types[i];
                row = i/4;
                column = i%4;
                GoodsTypeButton *btn = [[GoodsTypeButton alloc]initWithFrame:CGRectMake(rowMargin+(width+rowMargin)*column, 10+(height+colMargin)*row, width, height)];
                btn.titleLabel.font = Font(12);
                [btn setTitle:model.productTypeName forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                btn.backgroundColor = RandomColor;
                [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
                btn.model = model;
                [self.view addSubview:btn];
            }
            [self.firstypes addObjectsFromArray:types];
        }else{
            [MBProgressHUD showError:msg];
        }
    }];
}

- (NSMutableArray *)firstypes
{
    if (!_firstypes) {
        _firstypes = [NSMutableArray array];
    }
    return _firstypes;
}

@end
